<!DOCTYPE html>
<html>
	<head>
		<title>api访问记录-列表</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css">
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<style>
			.req-type-box{color: #FFF;}
			.req-type-box b{padding: 3px 7px; border-radius: 3px; font-size: 12px;}
			/* .req-api span{background-color: ; color: #409EFF; border: 1px #409EFF solid; border-radius: 2px; padding: 3px 5px;} */
			.req-api span{color: #44f; font-weight: 700; margin-left: 3px;}
			.req-p{display: inline-block; line-height: 1.4; padding: 2px 4px; border-radius: 4px; cursor: pointer;background-color: #fff2f4; color: #c7254e;}
			.req-ip{background-color: ; color: #409EFF; border: 1px #409EFF solid; border-radius: 2px; padding: 3px 5px;}
			
			.req-string{line-height: 1.4; padding: 2px 4px; border-radius: 4px;cursor: pointer; background-color: #ECF5FF;}
			
		</style>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<div class="c-panel">
				<!-- 参数栏 -->
				<div class="c-title">检索参数</div>
				<el-form size="mini" :inline="true" @submit.native.prevent>
					<el-form-item label="记录id：">
						<el-input v-model="p.id" placeholder="精确定位"></el-input>
					</el-form-item>
					<el-form-item label="请求ip：">
						<el-input v-model="p.reqIp" placeholder="IP筛选"></el-input>
					</el-form-item>
					<el-form-item label="请求api：">
						<el-input v-model="p.reqApi" placeholder="API接口筛选"></el-input>
					</el-form-item>
					<el-form-item style="min-width: 0px;">
						<el-button type="primary" icon="el-icon-search" @click="p.pageNo = 1; f5()">查询</el-button>
					</el-form-item>
					<br>
					<el-form-item label="请求token：">
						<el-input v-model="p.reqToken" placeholder="定向跟踪token"></el-input>
					</el-form-item>
					<el-form-item label="userId：">
						<el-input v-model="p.userId" placeholder="定向跟踪用户"></el-input>
					</el-form-item>
					<el-form-item label="adminId：">
						<el-input v-model="p.adminId" placeholder="定向跟踪用户"></el-input>
					</el-form-item>
					<br>
					<el-form-item label="res状态码：">
						<el-input v-model="p.resCode" placeholder="状态码筛选"></el-input>
					</el-form-item>
					<el-form-item label="请求时间：">
						<el-date-picker v-model="p.sTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="开始日期"></el-date-picker>
						-
						<el-date-picker v-model="p.eTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="结束日期"></el-date-picker>
					</el-form-item>
					<br />
					<el-form-item label="综合排序：" class="s-radio-text">
						<el-radio-group v-model="p.sortType">
							<el-radio :label="0">默认</el-radio>
							<el-radio :label="1">请求时间</el-radio>
							<el-radio :label="2">请求耗时</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-form>
				<!-- ------------- 快捷按钮 ------------- -->
				<div class="fast-btn" style="margin-top: -10px;">
					<el-button size="mini" type="primary" icon="el-icon-plus" @click="copyAll()">复制全部</el-button>
					<!-- <el-button size="mini" type="success" icon="el-icon-edit" @click="update($refs['data-table'].selection[0])"
						:disabled="!$refs['data-table'] || $refs['data-table'].selection.length != 1">修改</el-button> -->
					<el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteByIds()">删除</el-button>
					<el-button size="mini" type="warning" icon="el-icon-download" @click="sa.exportExcel()">导出</el-button>
					<el-button size="mini" type="info"  icon="el-icon-refresh"  @click="sa.f5()">重置</el-button>
					<el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteByStartEnd()">范围删除</el-button>
					<el-button size="mini" type="success" icon="el-icon-view" @click="f5StaData()">统计数据</el-button>
				</div>
				<!-- ------------- 数据列表 ------------- -->
				<el-table class="data-table" ref="data-table" :data="dataList" size="small">
					<el-table-column type="selection" width="45px"></el-table-column>
					
					<el-table-column label="请求id" width="110px">
						<template slot-scope="s">
							<div style="font-weight: bold;">{{s.row.id}}</div>
						</template>
					</el-table-column>
					<el-table-column label="请求ip" width="130px">
						<template slot-scope="s">
							<span class="req-ip">{{(s.row.reqIp)}}</span>
						</template>
					</el-table-column>
					<el-table-column label="请求方式" width="100px">
						<template slot-scope="s">
							<span class="req-type-box">
								<b style="background-color: #00A65A;" v-if=" s.row.reqType == 'GET' ">GET</b>
								<b style="background-color: #0073B7;" v-else-if=" s.row.reqType == 'POST' ">POST</b>
								<b style="background-color: #FF6A00;" v-else>{{s.row.reqType}}</b>
							</span>
						</template>
					</el-table-column>
					<el-table-column label="请求接口" width="250px">
						<template slot-scope="s">
							<p class="req-api"><span>{{sa.maxLength(s.row.reqApi)}}</bspan>
							</p>
							<span class="req-p" @click="seeReqParame(s.row)">{{sa.maxLength(s.row.reqParame, 70)}}</span>
						</template>
					</el-table-column>
					<el-table-column label="请求返回" min-width="370px">
						<template slot-scope="s">
							<p style="padding-left: 3px;">
								<b style="color: green;" v-if="s.row.resCode == 200">{{s.row.resCode}} - {{s.row.resMsg}}</b>
								<b style="color: red;" v-else-if="s.row.resCode == 500 || s.row.resCode == 501">{{s.row.resCode}} - {{s.row.resMsg}}</b>
								<b style="color: blue;" v-else>{{s.row.resCode}} - {{s.row.resMsg}}</b>
							</p>
							<p class="req-string" @click="seeResString(s.row)">
								{{sa.maxLength(s.row.resString, 70)}}
							</p>
						</template>
					</el-table-column>
					<el-table-column label="请求账号" width="150px">
						<template slot-scope="s">
							<p v-if="s.row.userId == 0">userId：&nbsp;&nbsp;&nbsp;0</p>
							<p v-else>userId：&nbsp;&nbsp;&nbsp;<el-link @click="sa.alert(s.row.userId)">{{s.row.userId}}</el-link>
							</p>
							<p v-if="s.row.adminId == 0">adminId：0</p>
							<p v-else>adminId：<el-link @click="sa.$page.openAdminInfo(s.row.adminId)">{{s.row.adminId}}</el-link>
							</p>
						</template>
					</el-table-column>
					<el-table-column label="请求token" width="150px" v-if="!sa.isNull(p.reqToken) ">
						<template slot-scope="s">
							<div style="width: 130px;">{{s.row.reqToken}}</div>
						</template>
					</el-table-column>
					<el-table-column label="请求时间" width="280px">
						<template slot-scope="s">
							<p>
								开始：{{sa.forDate(s.row.startTime, 'yyyy-MM-dd HH:mm:ss.ms')}} -
								<b>{{sa.isNull(sa.forDate2(s.row.startTime), '无')}}</b>
							</p>
							<p>
								结束：{{sa.forDate(s.row.endTime, 'yyyy-MM-dd HH:mm:ss.ms')}} -
								<b style="color: green;">耗时：{{(s.row.costTime + 0.0) / 1000}}s</b>
							</p>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="120px">
						<template slot-scope="s">
							<el-button type="text" size="mini" @click="copy(s.row)">复制</el-button>
							<el-button type="text" size="mini" @click="del(s.row)" v-if="way == 1">删除</el-button>
						</template>
					</el-table-column>
				</el-table>
				<!-- 分页 -->
				<div class="page-box">
					<el-pagination 
						background 
						layout="total, prev, pager, next, sizes, jumper" 
						:current-page.sync="p.pageNo"
						:page-size.sync="p.pageSize" 
						:total="dataCount" 
						:page-sizes="[1, 5, 10, 20, 30, 40, 50, 100, 1000]" 
						@current-change="f5()"
						@size-change="f5()">
					</el-pagination>
				</div>
			</div>
		</div>
		<script>
			var app = new Vue({
				el: '.vue-box',
				data: {
					sa: sa,
					p: { // 查询参数  
						pageNo: 1,
						pageSize: 10,
						id: '',
						reqToken: '',
						reqIp: '',
						reqApi: '',
						resCode: '',
						userId: '',
						adminId: '',
						sTime: '',
						eTime: '',
						sortType: 0
					},
					way: sa.p('way', 1),
					dataCount: 0,
					dataList: [], // 数据集合
					isNewestSta: false,	// 当前是否为最新统计数据 
					staData: {
						cost_time_count: 0,		// 总计耗时 
					}
				},
				methods: {
					// 刷新
					f5: function() {
						sa.ajax('/SgApilog/getList', sa.removeNull(this.p), function(res) {
							this.dataList = res.data; // 数据
							this.dataCount = res.dataCount; // 数据总数 
							sa.f5TableHeight();		// 刷新表格高度 
							this.isNewestSta = false;
						}.bind(this));
					},
					// 统计数据
					f5StaData: function() {
						var fn = function() {
							var str = '<b>总计请求：' + this.dataCount + ' 次</b><br/>'
								+ '<b>总计耗时：' + getDuration(this.staData.cost_time_count) + ' </b>';
							str = '<big>' + str + '</big>'
							layer.alert(str, {title: '统计数据'});
						}.bind(this);
						if(this.isNewestSta) {
							fn()
						} else {
							sa.ajax('/SgApilog/staBy', sa.removeNull(this.p), function(res) {
								this.staData = res.data;
								this.isNewestSta = true;
								fn();
							}.bind(this));
						}
					},
					// 复制 
					copy: function(data) {
						// sa.showIframe('数据详情', 'api-log-info.html?id=' + data.id);
						sa.copyText(JSON.stringify(data));
						sa.ok2('已成功复制到剪贴板');
					},
					// 复制全部
					copyAll: function() {
						// sa.showIframe('数据详情', 'api-log-info.html?id=' + data.id);
						sa.copyText(JSON.stringify(this.dataList));
						sa.ok2('已成功复制到剪贴板');
					},
					// 查看：访问参数 
					seeReqParame: function(data) {
						var jsonStr = data.reqParame;
						jsonStr = JSON.stringify(JSON.parse(jsonStr), null, "\t");
						layer.prompt({
							title: '请求参数',
							shadeClose: true,	// 点击遮罩关闭 
							formType: 2,		// 多行输入 
							value: jsonStr,		// 要显示的字符串
							maxlength: 9999999999,	// 最大输入字符长度
							area: ['600px', '400px'],	// 弹窗尺寸
							yes: function(index, layero){
							    layer.close(index); //如果设定了yes回调，需进行手工关闭
							}
						})
					},
					// 查看：返回参数  
					seeResString: function(data) {
						var jsonStr = data.resString;
						jsonStr = JSON.stringify(JSON.parse(jsonStr), null, "\t");
						layer.prompt({
							title: '返回参数',
							shadeClose: true,	// 点击遮罩关闭 
							formType: 2,		// 多行输入 
							value: jsonStr,		// 要显示的字符串
							maxlength: 9999999999,	// 最大输入字符长度
							area: ['600px', '400px'],	// 弹窗尺寸
							yes: function(index, layero){
							    layer.close(index); //如果设定了yes回调，需进行手工关闭
							}
						})
					},
					// 删除  
					del: function(data) {
						sa.confirm('是否删除，此操作不可撤销', function() {
							sa.ajax('/SgApilog/delete?id=' + data.id, function(res) {
								sa.arrayDelete(this.dataList, data);
								sa.ok('删除成功');
								sa.f5TableHeight();		// 刷新表格高度 
							}.bind(this))
						}.bind(this));
					},
					// 批量删除
					deleteByIds: function() {
						// 获取选中元素的id列表
						let selection = this.$refs['data-table'].selection;
						let ids = sa.getArrayField(selection, 'id');
						if(selection.length == 0) {
							return sa.msg('请至少选择一条数据')
						}
						// 提交删除 
						sa.confirm('是否批量删除选中数据？此操作不可撤销', function() {
							sa.ajax('/SgApilog/deleteByIds', {ids: ids.join(',')}, function(res) {
								sa.arrayDelete(this.dataList, selection);
								sa.ok('删除成功');
								sa.f5TableHeight();		// 刷新表格高度 
							}.bind(this))
						}.bind(this));
					},
					// 批量删除
					deleteByStartEnd: function() {
						sa.showIframe('批量删除', 'api-log-list-delete.html', '600px', '550px');
					}
				},
				created: function() {
					this.f5();
					sa.onInputEnter();	// 监听表单动作 
				}
			})
			
			function getDuration(my_time) {
				var days = my_time / 1000 / 60 / 60 / 24;
				var daysRound = Math.floor(days);
				var hours = my_time / 1000 / 60 / 60 - (24 * daysRound);
				var hoursRound = Math.floor(hours);
				var minutes = my_time / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
				var minutesRound = Math.floor(minutes);
				var seconds = my_time / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
				seconds = parseInt(seconds);
				// console.log('转换时间:', daysRound + '天', hoursRound + '时', minutesRound + '分', seconds + '秒');
				// var time = hoursRound + ':' + minutesRound + ':' + seconds
				// return time;
				if(daysRound >= 1) {
					return daysRound + '天' + hoursRound + '小时';
				} else if(hoursRound >= 1) {
					return hoursRound + '小时' + hoursRound + '分';
				} else if(minutesRound >= 1) {
					return minutesRound + '分' + seconds + '秒';
				} else {
					return seconds + '秒';
				}
			}
		</script>
	</body>
</html>
